# encoding: utf-8
import datetime

from app.extensions import log, db
from flask_restplus import Resource, fields
from app.extensions.api import Namespace, abort, http_exceptions
from app.modules.common.utils import DbCommit, checkToken
from app.modules.common.schemas import BaseSchema
from . import parameters, schemas
from .models import PostponeRecord
from app.modules.apply.models import ApplyRecord

api = Namespace('postpone_record', description = "延期记录")  # pylint: disable=invalid-name


@api.route('/')
class PostponeRecordList(Resource):
    @api.parameters(parameters.PostponeRecordListParameters())
    @api.response(schemas.PostponeRecordListSchema(many = False))
    @checkToken
    def get(self, args, **kwargs):
        """
        延期记录列表
        :param args:
        :return:
        """
        log.info("============== start query postpone-record list ==============")
        _apply_id = args['apply_id']
        _postpone_id = args['postpone_id']
        log.info('============== apply_id is %s ==============' % _apply_id)
        log.info('============== postpone_id is %s ==============' % _postpone_id)

        _query = PostponeRecord.query

        if _apply_id is not None:
            _query = _query.filter_by(apply_id = _apply_id)
        if _postpone_id is not None:
            _query = _query.filter_by(id = _postpone_id)

        _list = _query.order_by(PostponeRecord.create_date.desc()).all()

        return {'success': True, 'msg': '', 'data': _list}


@api.route('/apply')
class PostponeApplyList(Resource):
    @api.parameters(parameters.PostponeApplyParameters())
    @api.response(BaseSchema(many = False))
    @checkToken
    def get(self, args, **kwargs):
        """
        申请延期
        :param args:
        :return:
        """
        log.info("============== start apply postpone ==============")
        apply_id = args['apply_id']
        agent_ip = args['agent_ip']
        user_id = args['user_id']
        user_name = args['user_name']
        plan_name = args['plan_name']
        from_date = args['from_date']
        to_date = args['to_date']
        desc = args['desc']
        log.info('============== apply_id is %s ==============' % apply_id)
        log.info('============== agent_ip is %s ==============' % agent_ip)
        log.info('============== user_id is %s ==============' % user_id)
        log.info('============== user_name is %s ==============' % user_name)
        log.info('============== plan_name is %s ==============' % plan_name)
        log.info('============== from_date is %s ==============' % from_date)
        log.info('============== to_date is %s ==============' % to_date)
        log.info('============== desc is %s ==============' % desc)

        _info = ApplyRecord.query.filter_by(record_id = apply_id).first()
        if _info is None:
            _msg = '申请记录不存在 record_id:%s' % apply_id
            log.error('============== %s ==============' % _msg)
            return {'success': False, 'msg': _msg}

        _info.expect_recover_date = to_date

        _record = PostponeRecord(user_id, user_name, apply_id, agent_ip, from_date, to_date, plan_name, desc)

        db.session.add(_record)
        return DbCommit()
